package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.imp;

import java.util.ArrayList;
import java.util.List;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.AdminUser;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleUser;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.User;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.AlarmService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.DeviceService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.UserDataService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.UserDAO;


public class UserDataServiceImp implements UserDataService{
	
	//TODO: Completo
	private SystemService systemservice;
	private UserDAO userdao;
	private User userlogged;
	
	@Override
	public List<DeviceService> findDevices() {
		List<Device> userdevices = userdao.getDevices(userlogged);
		return systemservice.getDeviceServices(userdevices);
	}

	@Override
	public boolean isAdmin() {
		if(userlogged == null)
		    return false;
		return userlogged instanceof AdminUser;
	}

	@Override
	public boolean isSimpleUser() {
		if(userlogged == null)
		    return false;
		return userlogged instanceof SimpleUser;
	}
	
	@Override
	public List<AlarmService> getActiveAlarmsService() {
		List<AlarmService> ret = new ArrayList<AlarmService>();
		for (DeviceService  dev : this.findDevices()) {
			ret.addAll(dev.getActiveDeviceAlarmsService());
		}
		return ret;
	}
	
	/**
	 * @return the systemservice
	 */
	public SystemService getSystemservice() {
		return systemservice;
	}

	/**
	 * @param systemservice the systemservice to set
	 */
	public void setSystemservice(SystemService systemservice) {
		this.systemservice = systemservice;
	}

	/**
	 * @return the userdao
	 */
	public UserDAO getUserdao() {
		return userdao;
	}
	/**
	 * @param userdao the userdao to set
	 */
	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}
	/**
	 * @return the userlogged
	 */
	public User getUserlogged() {
		return userlogged;
	}
	/**
	 * @param userlogged the userlogged to set
	 */
	public void setUserlogged(User userlogged) {
		this.userlogged = userlogged;
	}
}
